package file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) throws IOException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入一个要扫描的目录路径");
        String rootDirPath=scanner.next();

        File rootDir=new File(rootDirPath);
        if(!rootDir.isDirectory()){
            System.out.println("输出路径非法");
            return;
        }
        System.out.println("shu");
        String deleteFileName=scanner.next();
        List<File> list=new ArrayList<>();
        scanDir(rootDir,deleteFileName,list);
        for(File f:list){
            System.out.println(f.getCanonicalPath()+"该文件是否删除");
            String choice=scanner.next();
            if(choice.equals("Y")){
                f.delete();
                System.out.println(f.getCanonicalPath()+"该文件删除成功");
            }
        }
    }
    private static void scanDir(File rootDir,String deleteFileName,List<File> list){
        File[] files=rootDir.listFiles();
        for(File f:files){
            if(f.isFile()){
                if(f.getName().equals(deleteFileName)){
                    list.add(f);
                }
            }else if(f.isDirectory()){
                scanDir(f,deleteFileName,list);
            }
        }
    }
}
